package cn.edu.wynu.springbootlearn.controller;

import cn.edu.wynu.springbootlearn.bean.Movie;
import cn.edu.wynu.springbootlearn.bean.Student;
import cn.edu.wynu.springbootlearn.mapper.MovieMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
public class MyFirstHttpInterface {

    // 自动注入：可以自动从容器里面取出一个Bean
    @Autowired
    Student student;

    @Autowired
    DataSource dataSource;

    @Autowired
    MovieMapper movieMapper;

    @GetMapping("/hello")
    public Student hello(){
        return student;
    }

    @GetMapping("/movie")
    public Movie queryMovie() throws SQLException {
        ResultSet resultSet = dataSource.getConnection().prepareCall("select * from movies where id = 1").executeQuery();
        resultSet.next();
        String id = resultSet.getString(1);
        System.out.println(id);
        String movieName = resultSet.getString(2);
        System.out.println(movieName);
        return new Movie();
    }

    @GetMapping("/allMovie")
    public List<Movie> allMovie() {
        return movieMapper.allMovie();
    }
}
